import 'package:flutter/material.dart';
import 'package:flutter/gestures.dart';

class GestureRecognizerDemo extends StatefulWidget {
  const GestureRecognizerDemo({super.key});

  @override
  State<GestureRecognizerDemo> createState() => _GestureRecognizerDemoState();
}

class _GestureRecognizerDemoState extends State<GestureRecognizerDemo> {
  final TapGestureRecognizer _tapRecognizer = TapGestureRecognizer();
  final LongPressGestureRecognizer _longPressRecognizer = LongPressGestureRecognizer();
  bool _isHighlighted = false;
  bool _isUnderlined = false;

  @override
  void dispose() {
    _tapRecognizer.dispose();
    _longPressRecognizer.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        RichText(
          text: TextSpan(
            style: const TextStyle(fontSize: 18, color: Colors.black),
            children: [
              const TextSpan(text: "This is a "),
              TextSpan(
                text: "clickable",
                style: TextStyle(
                  color: _isHighlighted ? Colors.blue : Colors.red,
                  fontWeight: FontWeight.bold,
                  decoration: _isUnderlined 
                      ? TextDecoration.underline 
                      : TextDecoration.none,
                ),
                recognizer: _tapRecognizer
                  ..onTap = () {
                    setState(() {
                      _isHighlighted = !_isHighlighted;
                    });
                  },
              ),
              const TextSpan(text: " word in a sentence."),
            ],
          ),
        ),
        const SizedBox(height: 20),
        RichText(
          text: TextSpan(
            style: const TextStyle(fontSize: 16, color: Colors.black),
            children: [
              const TextSpan(text: "Try: "),
              TextSpan(
                text: "Tap",
                style: const TextStyle(color: Colors.blue),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(content: Text("Tap recognized")));
                  },
              ),
              const TextSpan(text: " or "),
              TextSpan(
                text: "Long Press",
                style: const TextStyle(color: Colors.green),
                recognizer: LongPressGestureRecognizer()
                  ..onLongPress = () {
                    ScaffoldMessenger.of(context).showSnackBar(
                      const SnackBar(content: Text("Long press recognized")));
                  },
              ),
              const TextSpan(text: " these words."),
            ],
          ),
        ),
      ],
    );
  }
}